package com.canca.web.bean.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.canca.web.util.XmlUtil;

/**
 * 基本数据类型转换
 * @author canca trovals
 *
 */
public class ConvertUtil {
	private final static Map<String,String> convertTypesMap = new HashMap<String,String>();
	private static ConvertUtil util = new ConvertUtil();
	private ConvertUtil(){}
	public static ConvertUtil getInstance(){
		return util;
	}
	private static Log log = LogFactory.getLog(ConvertUtil.class);
	static{
		convertTypesMap.put("java.lang.Boolean", "boolean");
		convertTypesMap.put("java.lang.Character", "char");
		convertTypesMap.put("java.lang.Byte", "byte");
		convertTypesMap.put("java.lang.Double", "double");
		convertTypesMap.put("java.lang.Float", "float");
		convertTypesMap.put("java.lang.Integer", "int");
		convertTypesMap.put("java.lang.Long", "long");
		convertTypesMap.put("java.lang.Short", "short");
		convertTypesMap.put("java.lang.String", "string");

		convertTypesMap.put("boolean","java.lang.Boolean");
		convertTypesMap.put("char","java.lang.Character");
		convertTypesMap.put("byte","java.lang.Byte");
		convertTypesMap.put("double","java.lang.Double");
		convertTypesMap.put("float","java.lang.Float");
		convertTypesMap.put("int","java.lang.Integer");
		convertTypesMap.put("long","java.lang.Long");
		convertTypesMap.put("short","java.lang.Short");
		convertTypesMap.put("string", "java.lang.String");
	}
	
	/**
	 * 基本数据类型转换
	 * @param original	原数据
	 * @param classObj	要转换的类型
	 * @return
	 */
	public Object convert(Object original,Class classObj ){
		String className = classObj.getName();
		return convert(original,className);
	}
	/**
	 * 基本数据类型转换
	 * @param original	原数据
	 * @param classObj	要转换的类型
	 * @return
	 */
	public Object convert(Object original,String targetTypeString){
		String obj = convertTypesMap.get(targetTypeString);
		if(obj == null)return original;
		String originalString = original+"";
		try{
			if(obj.equals("java.lang.Boolean") || obj.equals("boolean")){
				return Boolean.valueOf(originalString);
			}else if(obj.equals("java.lang.Character") || obj.equals("char")){
				return originalString.charAt(0);
			}else if(obj.equals("java.lang.Byte") || obj.equals("byte")){
				return Byte.valueOf(originalString);
			}else if(obj.equals("java.lang.Double") || obj.equals("double")){
				return Double.valueOf(originalString);
			}else if(obj.equals("java.lang.Float") || obj.equals("float")){
				return Float.valueOf(originalString);
			}else if(obj.equals("java.lang.Integer") || obj.equals("int")){
				return Integer.parseInt(originalString);
			}else if(obj.equals("java.lang.Long") || obj.equals("long")){
				return Long.valueOf(originalString);
			}else if(obj.equals("java.lang.Short") || obj.equals("short")){
				return Short.valueOf(originalString);
			}else if(obj.equals("java.lang.String") || obj.equals("string")){
				return originalString;
			}
		}catch(Exception ex){
			if(log.isErrorEnabled())
				log.error("基本数据类型转换错误.",ex);
		}
		return original;
	}
}
